package GUI;

import modelo.gestionReserva.*;

import java.text.DateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.ComponentOrientation;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.ListSelectionModel;

import aplicaciones.Main;

public class ReservaGrafica {

	private JFrame frameReservas = null; // @jve:decl-index=0:visual-constraint="10,6"

	private JPanel panelReservas = null;

	private JPanel panelTablaReserva = null;

	private JScrollPane jScrollPane = null;

	private ListaReservas listaReservas = new ListaReservas();

	private JTable tablaReservas = null;

	private JLabel labelReservas = null;

	private JPanel botoneraModReserva = null;

	private JPanel panelBotonera = null;

	private JButton botonNuevaReserva = null;

	private JButton botonGuardar = null;

	private JPanel panelBox = null;

	private JButton botonSalir = null;

	private JLabel labelCliente = null;

	private JTextField textNombreCliente = null;

	private JPanel panelDatos = null;

	private JLabel labelFecha = null;

	private JTextField textFecha = null;

	private JLabel labelHora = null;

	private JTextField textHora = null;

	private JLabel labelComensales = null;

	private JSpinner spinComen = null;

	private JLabel labelMesas = null;

	private JComboBox comboMesas = null;

	private JButton botonModReserva = null;

	private JFrame framePpal = null;

	private JDialog dialogoNuevaReserva = null; // @jve:decl-index=0:visual-constraint="715,133"

	private JPanel panelNuevaReserva = null;

	private JPanel panelDatosNR = null;

	private JLabel labelNCliente = null;

	private JTextField textNombreNCliente = null;

	private JLabel labelNFecha = null;

	private JTextField textNFecha = null;

	private JLabel labelNHora = null;

	private JTextField textNHora = null;

	private JLabel labelNComensales = null;

	private JSpinner spinNComen = null;

	private JLabel labelNMesas = null;

	private JComboBox comboNMesas = null;

	private JButton botonNReserva = null;

	private JButton botonNCancelar = null;

	private JButton botonEliminar = null;

	public ReservaGrafica(JFrame fr) {
		JFrame frameRG = getFrameReservas();
		frameRG.setLocationRelativeTo(null);
		frameRG.setVisible(true);
		framePpal = fr;
		framePpal.setEnabled(false);

		/*
		 * Reserva m_Reservas[] = Main.m_Restaurante.getReservas();
		 *
		 * for(int i=0;i<m_Reservas.length;i++){ String[] str =
		 * {m_Reservas[i].getNombreCliente(),
		 * m_Reservas[i].getFecha().toString(),
		 * m_Reservas[i].getHora().toString(),
		 * Integer.toString(m_Reservas[i].getComensales()),
		 * Integer.toString(m_Reservas[i].getMesas().length)};
		 * listaReservas.insertRow(i,str); }
		 */

		Reserva m_Reservas[] = Main.m_Restaurante.getReservas().toArray(
				new Reserva[0]);
		

		for (int i = 0; i < m_Reservas.length; i++) {
			
			String mesas = "";
			
			for (int j=0; j<m_Reservas[i].getMesas().length ;j++){
				mesas +=  m_Reservas[i].getMesas()[j].toString() + "  ";
			}
			
			String[] str = { m_Reservas[i].getNombreCliente(),
					m_Reservas[i].getFecha().toString(),
					m_Reservas[i].getHora().toString(),
					Integer.toString(m_Reservas[i].getComensales()),
					mesas };
			listaReservas.insertRow(i, str);
		}
	}

	/**
	 * This method initializes frameReservas
	 *
	 * @return javax.swing.JFrame
	 */
	private JFrame getFrameReservas() {
		if (frameReservas == null) {
			frameReservas = new JFrame();
			frameReservas.setSize(new Dimension(702, 468));
			frameReservas.setTitle("Menú Reservas");
			frameReservas.setResizable(false);
			frameReservas.setContentPane(getPanelReservas());
			frameReservas.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					framePpal.setEnabled(true);
				}
			});
		}
		return frameReservas;
	}

	/**
	 * This method initializes panelReservas
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelReservas() {
		if (panelReservas == null) {
			panelReservas = new JPanel();
			panelReservas.setLayout(new BorderLayout());
			panelReservas.add(getPanelTablaReserva(), BorderLayout.WEST);
			panelReservas.add(getPanelBotonera(), BorderLayout.EAST);
		}
		return panelReservas;
	}

	/**
	 * This method initializes panelTablaReserva
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelTablaReserva() {
		if (panelTablaReserva == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setVgap(5);
			labelReservas = new JLabel();
			labelReservas.setText("Clientes con Reserva:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = -1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridx = -1;
			panelTablaReserva = new JPanel();
			panelTablaReserva.setLayout(borderLayout);
			panelTablaReserva.setPreferredSize(new Dimension(500, 500));
			panelTablaReserva.add(getJScrollPane(), BorderLayout.CENTER);
			panelTablaReserva.add(labelReservas, BorderLayout.NORTH);
			panelTablaReserva.add(getBotoneraModReserva(), BorderLayout.SOUTH);
		}
		return panelTablaReserva;
	}

	/**
	 * This method initializes jScrollPane
	 *
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getTablaReservas());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes tablaReservas
	 *
	 * @return javax.swing.JTable
	 */
	private JTable getTablaReservas() {
		if (tablaReservas == null) {
			tablaReservas = new JTable();
			tablaReservas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tablaReservas.setModel(listaReservas);
			tablaReservas.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {

					botonEliminar.setEnabled(true);
					textNombreCliente.setEnabled(true);
					textFecha.setEnabled(true);
					textHora.setEnabled(true);
					spinComen.setEnabled(true);
					comboMesas.setEnabled(true);
					botonModReserva.setEnabled(true);

					int fila = tablaReservas.getSelectedRow();
					textNombreCliente.setText((String) tablaReservas
							.getValueAt(fila, 0));
					textFecha.setText((String) tablaReservas
							.getValueAt(fila, 1));
					textHora
							.setText((String) tablaReservas.getValueAt(fila, 2));
					spinComen.setValue(Integer.parseInt((String) tablaReservas
							.getValueAt(fila, 3)));
					//comboMesas.addItem((String) tablaReservas.getValueAt(fila,
					//		4));

				}
			});
			listaReservas.addColumn("Cliente");
			listaReservas.addColumn("Fecha");
			listaReservas.addColumn("Hora");
			listaReservas.addColumn("Comensales");
			listaReservas.addColumn("Mesas");
		}
		return tablaReservas;
	}

	/**
	 * This method initializes botoneraModReserva
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getBotoneraModReserva() {
		if (botoneraModReserva == null) {
			labelCliente = new JLabel();
			labelCliente.setText("Nombre Cliente:");
			labelCliente.setPreferredSize(new Dimension(100, 16));
			botoneraModReserva = new JPanel();
			botoneraModReserva.setLayout(new BorderLayout());
			botoneraModReserva.setBorder(BorderFactory.createTitledBorder(null,
					"Modificar Reserva", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 12), new Color(51, 51, 51)));
			botoneraModReserva.add(getPanelDatos(), BorderLayout.WEST);
		}
		return botoneraModReserva;
	}

	/**
	 * This method initializes panelBotonera
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelBotonera() {
		if (panelBotonera == null) {
			panelBotonera = new JPanel();
			panelBotonera.setLayout(new BorderLayout());
			panelBotonera.add(getPanelBox(), BorderLayout.NORTH);
		}
		return panelBotonera;
	}

	/**
	 * This method initializes botonNuevaReserva
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonNuevaReserva() {
		if (botonNuevaReserva == null) {
			botonNuevaReserva = new JButton();
			botonNuevaReserva.setName("");
			botonNuevaReserva.setPreferredSize(new Dimension(140, 40));
			botonNuevaReserva.setText("Nueva Reserva...");
			
			botonNuevaReserva.setIcon( new ImageIcon(getClass().getResource("/iconos/table_add.png") ) );
			
			botonNuevaReserva
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							getDialogoNuevaReserva().setVisible(true);

						}
					});
		}
		return botonNuevaReserva;
	}

	/**
	 * This method initializes botonGuardar
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonGuardar() {
		if (botonGuardar == null) {
			botonGuardar = new JButton();
			botonGuardar.setText("Guardar");
			
			botonGuardar.setIcon( new ImageIcon(getClass().getResource("/iconos/save.gif") ) );
			
			botonGuardar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO
																// Auto-generated
																// Event stub
																// actionPerformed()
				}
			});
		}
		return botonGuardar;
	}

	/**
	 * This method initializes panelBox
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelBox() {
		if (panelBox == null) {
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 0;
			gridBagConstraints17.insets = new Insets(4, 0, 5, 20);
			gridBagConstraints17.anchor = GridBagConstraints.WEST;
			gridBagConstraints17.gridy = 1;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.insets = new Insets(5, 0, 0, 0);
			gridBagConstraints13.gridy = 3;
			gridBagConstraints13.ipadx = 71;
			gridBagConstraints13.ipady = 14;
			gridBagConstraints13.anchor = GridBagConstraints.WEST;
			gridBagConstraints13.gridx = 0;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.insets = new Insets(5, 0, 5, 0);
			gridBagConstraints12.gridy = 2;
			gridBagConstraints12.ipadx = 60;
			gridBagConstraints12.ipady = 14;
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.gridx = 0;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.insets = new Insets(20, 0, 5, 20);
			gridBagConstraints10.gridy = 0;
			gridBagConstraints10.anchor = GridBagConstraints.WEST;
			gridBagConstraints10.gridx = 0;
			GridLayout gridLayout3 = new GridLayout();
			gridLayout3.setRows(3);
			gridLayout3.setVgap(12);
			gridLayout3.setColumns(1);
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(3);
			gridLayout.setVgap(16);
			gridLayout.setHgap(0);
			panelBox = new JPanel();
			panelBox.setComponentOrientation(ComponentOrientation.UNKNOWN);
			panelBox.setLayout(new GridBagLayout());
			panelBox.add(getBotonNuevaReserva(), gridBagConstraints10);
			panelBox.add(getBotonGuardar(), gridBagConstraints12);
			panelBox.add(getBotonSalir(), gridBagConstraints13);
			panelBox.add(getBotonEliminar(), gridBagConstraints17);
		}
		return panelBox;
	}

	/**
	 * This method initializes botonSalir
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonSalir() {
		if (botonSalir == null) {
			botonSalir = new JButton();
			botonSalir.setText("Salir...");
			
			botonSalir.setIcon( new ImageIcon(getClass().getResource("/iconos/door.png") ) );
			
			botonSalir.setPressedIcon( new ImageIcon(getClass().getResource("/iconos/door_in.png") ) );
			
			botonSalir.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					framePpal.setEnabled(true);
					frameReservas.dispose();
				}
			});
		}
		return botonSalir;
	}

	/**
	 * This method initializes textNombreCliente
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextNombreCliente() {
		if (textNombreCliente == null) {
			textNombreCliente = new JTextField();
			textNombreCliente.setColumns(13);
			textNombreCliente.setEnabled(false);
		}
		return textNombreCliente;
	}

	/**
	 * This method initializes panelDatos
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelDatos() {
		if (panelDatos == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 3;
			gridBagConstraints11.anchor = GridBagConstraints.SOUTHEAST;
			gridBagConstraints11.insets = new Insets(6, 0, 0, 0);
			gridBagConstraints11.gridy = 2;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints9.gridy = 2;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.gridheight = 1;
			gridBagConstraints9.insets = new Insets(5, 4, 0, 0);
			gridBagConstraints9.ipadx = 60;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.insets = new Insets(9, 0, 0, 0);
			gridBagConstraints8.anchor = GridBagConstraints.NORTH;
			gridBagConstraints8.gridy = 2;
			labelMesas = new JLabel();
			labelMesas.setText("Mesas Reservadas:");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 3;
			gridBagConstraints7.fill = GridBagConstraints.NONE;
			gridBagConstraints7.ipadx = 21;
			gridBagConstraints7.anchor = GridBagConstraints.EAST;
			gridBagConstraints7.gridy = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2;
			gridBagConstraints6.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints6.anchor = GridBagConstraints.EAST;
			gridBagConstraints6.gridy = 1;
			labelComensales = new JLabel();
			labelComensales.setText("Comensales:");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(0, 4, 0, 0);
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
			gridBagConstraints41.gridx = 0;
			gridBagConstraints41.anchor = GridBagConstraints.WEST;
			gridBagConstraints41.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints41.gridy = 1;
			labelHora = new JLabel();
			labelHora.setText("Hora Reserva:");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new Insets(0, 7, 5, 0);
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridx = 3;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 2;
			gridBagConstraints3.ipadx = 0;
			gridBagConstraints3.insets = new Insets(0, 7, 5, 0);
			gridBagConstraints3.anchor = GridBagConstraints.EAST;
			gridBagConstraints3.gridy = 0;
			labelFecha = new JLabel();
			labelFecha.setText("Fecha Reserva:");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.ipadx = 8;
			gridBagConstraints2.ipady = 0;
			gridBagConstraints2.gridheight = 1;
			gridBagConstraints2.gridwidth = 1;
			gridBagConstraints2.insets = new Insets(0, 0, 5, 0);
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints.insets = new Insets(0, 4, 5, 0);
			gridBagConstraints.weightx = 1.0;
			panelDatos = new JPanel();
			panelDatos.setLayout(new GridBagLayout());
			panelDatos.add(labelCliente, gridBagConstraints2);
			panelDatos.add(getTextNombreCliente(), gridBagConstraints);
			panelDatos.add(labelFecha, gridBagConstraints3);
			panelDatos.add(getTextFecha(), gridBagConstraints4);
			panelDatos.add(labelHora, gridBagConstraints41);
			panelDatos.add(getTextHora(), gridBagConstraints5);
			panelDatos.add(labelComensales, gridBagConstraints6);
			panelDatos.add(getSpinComen(), gridBagConstraints7);
			panelDatos.add(labelMesas, gridBagConstraints8);
			panelDatos.add(getComboMesas(), gridBagConstraints9);
			panelDatos.add(getBotonModReserva(), gridBagConstraints11);
		}
		return panelDatos;
	}

	/**
	 * This method initializes textFecha
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextFecha() {
		if (textFecha == null) {
			textFecha = new JTextField();
			textFecha.setColumns(8);
			textFecha.setEnabled(false);
			textFecha.setText("");
		}
		return textFecha;
	}

	/**
	 * This method initializes textHora
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextHora() {
		if (textHora == null) {
			textHora = new JTextField();
			textHora.setColumns(5);
			textHora.setEnabled(false);
		}
		return textHora;
	}

	/**
	 * This method initializes spinComen
	 *
	 * @return javax.swing.JSpinner
	 */
	private JSpinner getSpinComen() {
		if (spinComen == null) {
			spinComen = new JSpinner();
			spinComen.setEnabled(false);
		}
		return spinComen;
	}

	/**
	 * This method initializes comboMesas
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboMesas() {
		if (comboMesas == null) {
			comboMesas = new JComboBox();
			comboMesas.setPreferredSize(new Dimension(21, 25));
			comboMesas.setEnabled(false);
			comboMesas.addItem("Automático");
			comboMesas.addItem("Manual");
		}
		return comboMesas;
	}

	/**
	 * This method initializes botonModReserva
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonModReserva() {
		if (botonModReserva == null) {
			botonModReserva = new JButton();
			botonModReserva.setText("Modificar");
			botonModReserva.setEnabled(false);
			botonModReserva
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							int comen;
							String n_cliente[] = new String[5];
							n_cliente[0] = textNombreCliente.getText();
							n_cliente[1] = textFecha.getText();
							n_cliente[2] = textHora.getText();
							n_cliente[3] = spinComen.getValue().toString();

							// HAY Q COMPROBAR Q LA FECHA Y LOS COMENSALES TIENE
							// FORMATO CORRECTO

							try {
								comen = Integer.parseInt(n_cliente[3]);
								n_cliente[4] = "Mesa 1"; // Hay que calcular
															// las mesas
							} catch (NumberFormatException ex) {
								comen = -1;
							}

							if ((n_cliente[0] != null && n_cliente[0].length() > 0)
									&& (n_cliente[1] != null && n_cliente[1]
											.length() > 0)
									&& (n_cliente[2] != null && n_cliente[2]
											.length() > 0) && comen > 0) {

								// AKI SE MODIFICA LA RESERVA

				        		Reserva res = Main
				        						.m_Restaurante
				        						.getReservas()
				        						.get(tablaReservas.getSelectedRow());

				        		res.setComensales(new Integer(n_cliente[3]));
				        		res.setNombreCliente(n_cliente[0]);
				        		
				        		//Main.m_Restaurante.agregarReserva(res);
				        		
				        		if (comboMesas.getSelectedIndex() == 1){
				        			JDialog j = new SeleccionManualMess(res);
				        			j.setModal(true);
				        			j.setVisible(true);
				        		}
				        		else {
				        			Reserva.seleccionarMesas(Main.m_Restaurante, res);
				        		}
				        			

				        		DateFormat df = DateFormat.getDateTimeInstance();
				        		try {
				        			res.setFecha(df.parse(n_cliente[1]+" "+n_cliente[2]));
				        		}
				        		catch (Exception pe) {
				        			System.out.println(pe);
				        		}

								listaReservas.setValueAt(n_cliente[0],
										tablaReservas.getSelectedRow(), 0);
								listaReservas.setValueAt(n_cliente[1],
										tablaReservas.getSelectedRow(), 1);
								listaReservas.setValueAt(n_cliente[2],
										tablaReservas.getSelectedRow(), 2);
								listaReservas.setValueAt(n_cliente[3],
										tablaReservas.getSelectedRow(), 3);
								
								String mesas = "";
								
								for (int i = 0; i<res.getMesas().length; ++i){
									mesas += res.getMesas()[i].toString() + "  ";
								}
								
								n_cliente[4] = mesas;
								
								listaReservas.setValueAt(n_cliente[4],
										tablaReservas.getSelectedRow(), 4);

							} else {
								JOptionPane
										.showMessageDialog(
												null,
												"Algún campo es incorrecto o está vacio",
												"Error",
												JOptionPane.ERROR_MESSAGE);
							}

						}
					});
		}
		return botonModReserva;
	}

	/**
	 * This method initializes dialogoNuevaReserva
	 *
	 * @return javax.swing.JDialog
	 */
	private JDialog getDialogoNuevaReserva() {
		if (dialogoNuevaReserva == null) {
			dialogoNuevaReserva = new JDialog(getFrameReservas());
			dialogoNuevaReserva.setSize(new Dimension(310, 251));
			dialogoNuevaReserva.setTitle("Nueva Reserva");
			dialogoNuevaReserva.setModal(true);
			dialogoNuevaReserva.setResizable(false);
			dialogoNuevaReserva.setLocationRelativeTo(frameReservas);
			dialogoNuevaReserva.setContentPane(getPanelNuevaReserva());
		}
		return dialogoNuevaReserva;
	}

	/**
	 * This method initializes panelNuevaReserva
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelNuevaReserva() {
		if (panelNuevaReserva == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			gridLayout1.setVgap(0);
			gridLayout1.setHgap(0);
			panelNuevaReserva = new JPanel();
			panelNuevaReserva.setLayout(gridLayout1);
			panelNuevaReserva.add(getPanelDatosNR(), null);
		}
		return panelNuevaReserva;
	}

	/**
	 * This method initializes panelDatosNR
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelDatosNR() {
		if (panelDatosNR == null) {
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 1;
			gridBagConstraints16.insets = new Insets(15, 0, 0, 0);
			gridBagConstraints16.gridy = 5;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.gridx = 0;
			gridBagConstraints15.insets = new Insets(15, 0, 0, 0);
			gridBagConstraints15.gridy = 5;
			GridBagConstraints gridBagConstraints91 = new GridBagConstraints();
			gridBagConstraints91.anchor = GridBagConstraints.CENTER;
			gridBagConstraints91.insets = new Insets(0, 0, 8, 0);
			gridBagConstraints91.gridheight = 1;
			gridBagConstraints91.gridx = 1;
			gridBagConstraints91.gridy = 4;
			gridBagConstraints91.ipadx = 60;
			gridBagConstraints91.weightx = 1.0;
			gridBagConstraints91.fill = GridBagConstraints.VERTICAL;
			GridBagConstraints gridBagConstraints81 = new GridBagConstraints();
			gridBagConstraints81.anchor = GridBagConstraints.WEST;
			gridBagConstraints81.gridx = 0;
			gridBagConstraints81.gridy = 4;
			gridBagConstraints81.insets = new Insets(0, 0, 8, 0);
			labelNMesas = new JLabel();
			labelNMesas.setText("Mesas Reservadas:");
			GridBagConstraints gridBagConstraints71 = new GridBagConstraints();
			gridBagConstraints71.anchor = GridBagConstraints.WEST;
			gridBagConstraints71.gridx = 1;
			gridBagConstraints71.gridy = 2;
			gridBagConstraints71.ipadx = 21;
			gridBagConstraints71.insets = new Insets(0, 0, 8, 0);
			gridBagConstraints71.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
			gridBagConstraints61.anchor = GridBagConstraints.WEST;
			gridBagConstraints61.gridx = 0;
			gridBagConstraints61.gridy = 2;
			gridBagConstraints61.insets = new Insets(0, 0, 8, 0);
			labelNComensales = new JLabel();
			labelNComensales.setText("Comensales:");
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			gridBagConstraints51.anchor = GridBagConstraints.WEST;
			gridBagConstraints51.insets = new Insets(0, 0, 8, 0);
			gridBagConstraints51.gridx = 1;
			gridBagConstraints51.gridy = 3;
			gridBagConstraints51.weightx = 1.0;
			gridBagConstraints51.fill = GridBagConstraints.VERTICAL;
			GridBagConstraints gridBagConstraints411 = new GridBagConstraints();
			gridBagConstraints411.anchor = GridBagConstraints.WEST;
			gridBagConstraints411.gridx = 0;
			gridBagConstraints411.gridy = 3;
			gridBagConstraints411.insets = new Insets(0, 0, 8, 0);
			labelNHora = new JLabel();
			labelNHora.setText("Hora Reserva:");
			GridBagConstraints gridBagConstraints42 = new GridBagConstraints();
			gridBagConstraints42.anchor = GridBagConstraints.WEST;
			gridBagConstraints42.insets = new Insets(0, 0, 7, 0);
			gridBagConstraints42.gridx = 1;
			gridBagConstraints42.gridy = 1;
			gridBagConstraints42.weightx = 1.0;
			gridBagConstraints42.fill = GridBagConstraints.VERTICAL;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.anchor = GridBagConstraints.WEST;
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.gridy = 1;
			gridBagConstraints31.ipadx = 0;
			gridBagConstraints31.insets = new Insets(0, 0, 7, 0);
			labelNFecha = new JLabel();
			labelNFecha.setText("Fecha Reserva:");
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints14.weightx = 1.0;
			gridBagConstraints14.anchor = GridBagConstraints.WEST;
			gridBagConstraints14.ipadx = 0;
			gridBagConstraints14.ipady = 0;
			gridBagConstraints14.weighty = 0.0;
			gridBagConstraints14.insets = new Insets(0, 0, 8, 0);
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.gridheight = 1;
			gridBagConstraints21.gridwidth = 1;
			gridBagConstraints21.ipadx = 0;
			gridBagConstraints21.ipady = 0;
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.gridy = 0;
			gridBagConstraints21.weighty = 0.0;
			gridBagConstraints21.insets = new Insets(0, 0, 8, 0);
			labelNCliente = new JLabel();
			labelNCliente.setPreferredSize(new Dimension(100, 16));
			labelNCliente.setText("Nombre Cliente:");
			panelDatosNR = new JPanel();
			panelDatosNR.setLayout(new GridBagLayout());
			panelDatosNR.setBorder(BorderFactory.createTitledBorder(null,
					"Nueva Reserva", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 12), new Color(51, 51, 51)));
			panelDatosNR.add(labelNCliente, gridBagConstraints21);
			panelDatosNR.add(getTextNombreNCliente(), gridBagConstraints14);
			panelDatosNR.add(labelNFecha, gridBagConstraints31);
			panelDatosNR.add(getTextNFecha(), gridBagConstraints42);
			panelDatosNR.add(labelNHora, gridBagConstraints411);
			panelDatosNR.add(getTextNHora(), gridBagConstraints51);
			panelDatosNR.add(labelNComensales, gridBagConstraints61);
			panelDatosNR.add(getSpinNComen(), gridBagConstraints71);
			panelDatosNR.add(labelNMesas, gridBagConstraints81);
			panelDatosNR.add(getComboNMesas(), gridBagConstraints91);
			panelDatosNR.add(getBotonNReserva(), gridBagConstraints15);
			panelDatosNR.add(getBotonNCancelar(), gridBagConstraints16);
		}
		return panelDatosNR;
	}

	/**
	 * This method initializes textNombreNCliente
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextNombreNCliente() {
		if (textNombreNCliente == null) {
			textNombreNCliente = new JTextField();
			textNombreNCliente.setEnabled(true);
			textNombreNCliente.setColumns(13);
			textNombreNCliente.setText("");
		}
		return textNombreNCliente;
	}

	/**
	 * This method initializes textNFecha
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextNFecha() {
		if (textNFecha == null) {
			textNFecha = new JTextField();
			textNFecha.setEnabled(true);
			textNFecha.setColumns(8);
			textNFecha.setText("");
		}
		return textNFecha;
	}

	/**
	 * This method initializes textNHora
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextNHora() {
		if (textNHora == null) {
			textNHora = new JTextField();
			textNHora.setEnabled(true);
			textNHora.setColumns(5);
			textNHora.setText("");
		}
		return textNHora;
	}

	/**
	 * This method initializes spinNComen
	 *
	 * @return javax.swing.JSpinner
	 */
	private JSpinner getSpinNComen() {
		if (spinNComen == null) {
			spinNComen = new JSpinner();
			spinNComen.setEnabled(true);
			spinNComen.setValue(0);
		}
		return spinNComen;
	}

	/**
	 * This method initializes comboNMesas
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboNMesas() {
		if (comboNMesas == null) {
			comboNMesas = new JComboBox();
			comboNMesas.setEnabled(true);
			comboNMesas.setEditable(false);
			comboNMesas.setPreferredSize(new Dimension(21, 24));
			comboNMesas.addItem("Automático");
			comboNMesas.addItem("Manual");
		}
		return comboNMesas;
	}

	/**
	 * This method initializes botonNReserva
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonNReserva() {
		if (botonNReserva == null) {
			botonNReserva = new JButton();
			botonNReserva.setText("Aceptar");
			botonNReserva
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							int comen;
							String n_cliente[] = new String[5];
							n_cliente[0] = textNombreNCliente.getText();
							n_cliente[1] = textNFecha.getText();
							n_cliente[2] = textNHora.getText();
							n_cliente[3] = spinNComen.getValue().toString();

							// HAY Q COMPROBAR Q LA FECHA Y LOS COMENSALES TIENE
							// FORMATO CORRECTO

							try {
								comen = Integer.parseInt(n_cliente[3]);
							} catch (NumberFormatException ex) {
								comen = -1;
							}
							

							if ((n_cliente[0] != null && n_cliente[0].length() > 0)
									&& (n_cliente[1] != null && n_cliente[1]
											.length() > 0)
									&& (n_cliente[2] != null && n_cliente[2]
											.length() > 0) && comen > 0) {

								// AKI SE AÑADE LA RESERVA
				        		Date dr=null;
				        		DateFormat df = DateFormat.getDateTimeInstance();
				        		try {
				        			dr=df.parse(textNFecha.getText()+" "+textNHora.getText());
				        		}
				        		catch (Exception pe) {
				        			System.out.println(pe);
				        		}

				        		Reserva r = new Reserva(0,
				        				(Integer)spinNComen.getValue(),
				        				dr,
				        				dr,
				        				textNombreNCliente.getText(), Main.m_Restaurante.getIPServidorRMI());

				        		Main.m_Restaurante.agregarReserva(r);
				        		
				        		if (comboNMesas.getSelectedIndex() == 1){
				        			JDialog j = new SeleccionManualMess(r);
				        			j.setModal(true);
				        			j.setVisible(true);
				        		}
				        		else {
				        			Reserva.seleccionarMesas(Main.m_Restaurante, r);
				        		}

				        		
				        		String mesas = "";
								
				        		for(int i=0; i<r.getMesas().length; ++i){
				        			mesas += r.getMesas()[i].toString() + "  ";
				        		}
				        		
								n_cliente[4] = mesas; // Hay que calcular
								// las mesas
				        		
				        		
								listaReservas.insertRow(listaReservas
										.getRowCount(), n_cliente);

								dialogoNuevaReserva.dispose();
								textNombreNCliente.setText("");
								textNFecha.setText("");
								textNHora.setText("");
								spinNComen.setValue(0);

							} else {
								JOptionPane
										.showMessageDialog(
												null,
												"Algún campo es incorrecto o está vacio",
												"Error",
												JOptionPane.ERROR_MESSAGE);
							}
						}
					});
		}
		return botonNReserva;
	}

	/**
	 * This method initializes botonNCancelar
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonNCancelar() {
		if (botonNCancelar == null) {
			botonNCancelar = new JButton();
			botonNCancelar.setText("Cancelar");
			botonNCancelar
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							dialogoNuevaReserva.dispose();
						}
					});
		}
		return botonNCancelar;
	}

	/**
	 * This method initializes botonEliminar
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBotonEliminar() {
		if (botonEliminar == null) {
			botonEliminar = new JButton();
			botonEliminar.setText("Eliminar Reserva");
			botonEliminar.setEnabled(false);
			botonEliminar.setPreferredSize(new Dimension(140, 40));
			
			botonEliminar.setIcon( new ImageIcon(getClass().getResource("/iconos/table_delete.png") ) );
			
			botonEliminar
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {

							int resp = JOptionPane.showConfirmDialog(null,
									"¿ De verdad quiere eliminar la reserva ?",
									"Eliminar Reserva",
									JOptionPane.YES_NO_OPTION);

							if (resp == 0) {
				        		Reserva r = Main
			    				.m_Restaurante
			    				.getReservas()
			    				.get(tablaReservas.getSelectedRow());

			        		Main.m_Restaurante.cancelarReserva(r);

								// Eliminamos RESERVA SELECCIONADA

								listaReservas.removeRow(tablaReservas
										.getSelectedRow());
								textNombreCliente.setText("");
								textFecha.setText("");
								textHora.setText("");
								spinComen.setValue(0);
								//comboMesas.removeAllItems();

								textNombreCliente.setEnabled(false);
								textFecha.setEnabled(false);
								textHora.setEnabled(false);
								spinComen.setEnabled(false);
								comboMesas.setEnabled(false);
								botonModReserva.setEnabled(false);
								botonEliminar.setEnabled(false);

							}

						}
					});
		}
		return botonEliminar;
	}

}

class ListaReservas extends DefaultTableModel {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public boolean isCellEditable(int row, int column) {
		return false;
	}
}
